package com.jn.server;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.widget.ListView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";
    private IStudentService mService;
    private TextView sCount;
    private ListView sListView;
    private StudentAdapter adapter;
    private List<Student> studentList = new ArrayList<>();
    private ServiceConnection mConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.d(TAG, "onServiceConnected: Service connected.");
            mService = IStudentService.Stub.asInterface(service);
            try {
                int count = mService.getStudentCount();
                Log.d(TAG, "Initial student count: " + count);
                sCount.setText(String.valueOf(count));

                // 获取学生列表
                getStudentMessage();

                // 添加监听器
                mService.addListener(new IStudentChangeListener.Stub() {
                    @Override
                    public void onStudentCountChanged(int count) throws RemoteException {
                        Log.d(TAG, "Student count changed: " + count);
                        runOnUiThread(() -> {
                            sCount.setText(String.valueOf(count));
                            getStudentMessage();
                        });
                    }
                });
            } catch (RemoteException e) {
                Log.e(TAG, "RemoteException occurred while calling getStudentCount()", e);
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.d(TAG, "onServiceDisconnected: Service disconnected.");
            mService = null;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        sCount = findViewById(R.id.s_count);
        sListView = findViewById(R.id.s_list);
    }

    private void getStudentMessage() {
        try {
            if (mService != null) {
                int count = mService.getStudentCount();
                sCount.setText(String.valueOf(count));

                studentList = mService.getStudentList();
                if (adapter == null) {
                    adapter = new StudentAdapter(this, R.layout.item, studentList);
                    sListView.setAdapter(adapter);
                } else {
                    adapter.clear();
                    adapter.addAll(studentList);
                }
            } else {
                Log.w(TAG, "getStudentMessage: Service not bound yet.");
            }
        } catch (RemoteException e) {
            Log.e(TAG, "RemoteException occurred while calling getStudentList()", e);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        bindService();
    }

    private void bindService() {
        Log.d(TAG, "bindService: Attempting to bind service.");
        Intent intent = new Intent();
        intent.setPackage("com.jn.server");
        intent.setAction("com.jn.server.action");
        bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mConnection != null) {
            unbindService(mConnection);
        }
    }
}